import requests
from bs4 import BeautifulSoup
import time

base_url='http://m.lovefoux.com'

headers = {'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
               'Accept - Encoding':'gzip, deflate',
               'Accept-Language':'zh-Hans-CN, zh-Hans; q=0.5',
               'Connection':'Keep-Alive',
               'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}
imgurl_list=[]
count=0
mark=True
url='http://m.lovefoux.com/dongtaitu/10.html'
url_history=''
while(mark):
    try:

        r = requests.get(url)
        content=r.content
        soup = BeautifulSoup(content,"lxml")

        pa=soup.find('div',attrs={'class':'cont'})
        img_list=pa.find_all('img')

        for img in img_list:
            if(img['src'] not in imgurl_list):
                imgurl_list.append(img['src'])
                count=count+1
        


        a_set=soup.find('div',attrs={'class':'pagearti'}).find_all('a')


        #只有当找不到下页标签和链接重复时才推出循环
        mark=False
        for a in a_set:
            if '上页' in a.text:
                url_history=url
                url=base_url+a['href']
                mark=True
                break
        
        if url==url_history:
            mark=False
        if(len(imgurl_list)>136):
            print('strat write to disk...'+str(count))
            with open('lovefou.txt',mode='a+') as file:
                for imgurl in imgurl_list:
                    file.write(imgurl+'\n')
            imgurl_list=[]
            mark=False

        


    except Exception as e:
            print (e)
            print(count)
            print(url)
            mark=False
    finally:
        time.sleep(1)
        print(str(count),end='\r')

print('strat write to disk...'+str(count))
with open('lovefou.txt',mode='a+') as file:
    for imgurl in imgurl_list:
        file.write(imgurl+'\n')

# count=0
# for imgurl in imgurl_list[:10]:
#     print(imgurl)
#     r = requests.get(imgurl,headers=headers)
#     count=count+1
#     with open(str(count)+'.gif','wb') as file:
#         file.write(r.content)
